package poi;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 目的：创建一个单元格
 * 思路：首先创建一个workbook，然后创建一个sheet，其次再创建一行，在航中创建单元格并赋值
 * 最后将这些写入到文件中
 *
 * Illustrates how to create cell values.
 *
 * @author Glen Stampoultzis (glens at apache.org)
 */
public class CreateCells {
    public static void main(String[] args) throws IOException {

        //High level representation of a workbook. This is the first object most users will construct whether they are reading or writing a workbook.
        // It is also the top level object for creating new sheets/etc.
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("new sheet");

        // Create a row and put some cells in it. Rows are 0 based.
        HSSFRow row = sheet.createRow(0);
        // Create a cell and put a value in it.
        HSSFCell cell = row.createCell(0);
        cell.setCellValue(1);

        // Create another cells and put values in them
        row.createCell(1).setCellValue(1.2);
        row.createCell(2).setCellValue("This is a string");
        row.createCell(3).setCellValue(true);

        // Write the output to a file
        FileOutputStream fileOut = new FileOutputStream("D:/testData/sample.xls");
        wb.write(fileOut);
        fileOut.close();

        wb.close();
    }
}